<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <img src="../assets/images/logo.png" alt="">
      <el-menu
        mode="horizontal"
        background-color="#23262e"
        text-color="#fff"
        active-text-color="rgb(64,158,255)">
        <el-submenu index="three">
            <template slot="title">
              <img class="active" :src="user.user_pic" alt="" v-if="user.user_pic" />
              <img src="../assets/logo.png" alt="" class="active" v-else />
              个人中心
              </template>
            <el-menu-item index="c1"><i class="el-icon-edit"></i>重置密码</el-menu-item>
            <el-menu-item index="c2"><i class="el-icon-edit"></i>更换头像</el-menu-item>
            <el-menu-item index="c3"><i class="el-icon-edit"></i>用户资料</el-menu-item>
        </el-submenu>
        <el-menu-item index="1" @click="logout">退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <div class="user-info">
          <img class="active" :src="user.user_pic" alt="" v-if="user.user_pic" />
          <img src="../assets/logo.png" alt="" class="active" v-else />
          <p>欢迎你{{user.nickname || user.username}}</p>
        </div>
        <el-menu
         background-color="#23262e"
         text-color="#fff"
         active-text-color="rgb(64,158,255)"
         :default-active="$router.path"
         unique-opened
         router="">
          <template v-for="item in menu">
            <el-menu-item
              :key="item.indexPath"
              v-if="item.children === null"
              :index="item.indexPath">
              <i :class="item.icon"></i>
              {{ item.title }}
            </el-menu-item>
            <el-submenu :key="item.indexPath" :index="item.indexPath" v-else>
              <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
              <el-menu-item
               :index="v.indexPath"
               v-for="v in item.children"
               :key="v.indexPath">
               <i :class="v.icon"></i>
               {{v.title}}
            </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <!-- 内容区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import { getMenuAPI } from '@/api/menu'
export default {
  name: 'MyHome',
  data() {
    return {
      menu: []
    }
  },
  created() {
    // 利用生命周期自动调用vuex中的方法
    this.$store.dispatch('user/getUser')
    this.getMenu()
  },
  computed: {
    // ...mapState('模块名', ['', '', ''])
    ...mapState('user', ['user'])
  },
  methods: {
    logout() {
      // 询问是否要退出？如果确定退出，清除token、user，跳转到登录页
      this.$confirm('拜拜了您', '提示', {
        confirmButtonText: '确定', // 配置确认按钮的文字，默认是确定
        cancelButtonText: '取消', // 配置取消按钮的文字，默认是取消
        type: 'warning' // 弹框的类型，警告框
      })
        .then(() => {
          // 点击确认退出
          this.$store.commit('user/updateToken', '')
          this.$store.commit('user/updateUser', {})
          this.$router.push('/login')
        })
        .catch(() => {
          // 点击了取消
        })
    },
    // 获取菜单数据
    async getMenu() {
      // 调用 API 方法，获取数据
      const { data: res } = await getMenuAPI()
      // 将数据存储到当前数据的数据项中
      if (res.code === 0) {
        this.menu = res.data
      }
    }
  }
}
</script>

<style lang="less" scoped>
/* 头部 */
  .el-header {
    background-color: #23262e;
    height: 60px;
    display: flex;
    justify-content: space-between;
  }
  /* 大容器 */
  .el-container {
    height: 100%;
  }
  /* 侧边栏 */
  .el-aside {
    background-color: #23262e;
    height: 100%;
    .el-menu {
      width: 200px;
      overflow: hidden;
    }
  }
  .el-menu-item {
    font-size: 13px;
  }
  /* 内容区 */
  .el-main{
    background-color: #f2f2f2;
  }
  .active {
    height: 35px;
    width: 35px;
    margin-right: 15px;
    border-radius: 50%;
    background-color: #fff;
  }
  .el-submenu {
    /deep/ .el-submenu_title {
      font-size: 13px;
    }
  }
  .user-info {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 70px;
  p {
    color: #fff;
    font-size: 13px;
  }
}
</style>
